package com.help.help.service.impl;

import com.help.help.entity.CateEntity;
import com.help.help.service.CateService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.help.help.dao.CatelistDao;
import com.help.help.entity.CatelistEntity;
import com.help.help.service.CatelistService;


@Service("catelistService")
public class CatelistServiceImpl extends ServiceImpl<CatelistDao, CatelistEntity> implements CatelistService {
    @Autowired
    private CateService cateService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CatelistEntity> wrapper = new QueryWrapper<>();
        QueryWrapper<CateEntity> catewarper = new QueryWrapper<>();
        Object key = params.get("key");
        if(!StringUtil.isNullOrEmpty(key.toString())){
              catewarper.eq("name",key);
            CateEntity one = cateService.getOne(catewarper);
            if (one != null) {
                wrapper.eq("cate_id", one.getId());
            } else {
                wrapper.eq("name", key);
            }

        }
        IPage<CatelistEntity> page = this.page(
                new Query<CatelistEntity>().getPage(params),
              wrapper
        );
        List<CatelistEntity> records = page.getRecords();
        for (CatelistEntity catelist : records) {
              catelist.setCate(cateService.getById(catelist.getCateId()));
        }
          page.setRecords(records);
        return new PageUtils(page);
    }

}